<?php
/**
 * 函数配置文件
 * @author chenhao
 * @since 2010-01-25
 */


function getmicrotime() {
    list($usec, $sec) = explode(" ",microtime());
    return ((float)$usec + (float)$sec);
}

/*function uc2html($str) {
	$ret = '';
	for( $i=0; $i<strlen($str)/2; $i++ ) {
		$charcode = ord($str[$i*2])+256*ord($str[$i*2+1]);
		//$ret .= '&#'.$charcode;
		//$ret .= iconv("utf-8","gb2312",u2utf8($charcode));
		$ret .= u2utf8($charcode);
	}
	return $ret;
}*/

/**
 * 解决中文与数字字母混合问题
 *
 * @param string $str
 * @return string
 */
function uc2html($str) {
   $ret = ''; 
   for( $i=0; $i<strlen($str)/2; $i++ ) { 
       $charcode = ord($str[$i*2])+256*ord($str[$i*2+1]); 
       // $ret .= '&#'.$charcode; 
       if($charcode<127) 
         $ret .=chr($charcode);
       else
         //$ret .= iconv("utf-8","gb2312",u2utf8($charcode)); //GB2312编码输出
         $ret .= u2utf8($charcode); //UTF-8编码输出
   }
       return $ret;
}

/**
* 转换unicode十进制内码为utf-8编码
*/
function u2utf8($c) {
	$str="";
	if ($c < 0x80) {
		$str.=$c;
	} else if ($c < 0x800) {
		$str.=chr(0xC0 | $c>>6);
		$str.=chr(0x80 | $c & 0x3F);
	} else if ($c < 0x10000) {
		$str.=chr(0xE0 | $c>>12);
		$str.=chr(0x80 | $c>>6 & 0x3F);
		$str.=chr(0x80 | $c & 0x3F);
	} else if ($c < 0x200000) {
		$str.=chr(0xF0 | $c>>18);
		$str.=chr(0x80 | $c>>12 & 0x3F);
		$str.=chr(0x80 | $c>>6 & 0x3F);
		$str.=chr(0x80 | $c & 0x3F);
	}
	return $str;
}

function show_time() {
	global $time_start,$time_end;

	$time = $time_end - $time_start;
	echo "Parsing done in $time seconds<hr size=1><br>";
}

function fatal($msg = '') {
	echo '[Fatal error]';
	if( strlen($msg) > 0 )
		echo ": $msg";
	echo "<br>\nScript terminated<br>\n";
	if( $f_opened) @fclose($fh);
	exit();
};